package com.spzx.cart.api.factory;

import com.spzx.cart.api.RemoteCartService;
import com.spzx.cart.api.domain.CartInfo;
import com.spzx.common.core.domain.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 购物车远程接口 降级处理
 */
@Component
public class RemoteCartFallbackFactory implements FallbackFactory<RemoteCartService> {

    private static final Logger log = LoggerFactory.getLogger(RemoteCartFallbackFactory.class);

    @Override
    public RemoteCartService create(Throwable throwable) {
        log.error("购物车远程接口调用失败:{}", throwable.getMessage());
        return new RemoteCartService() {
            @Override
            public R<List<CartInfo>> getCartCheckedList(String source) {
                return R.fail("获取用户购物车选中的商品列表失败:" + throwable.getMessage());
            }

            @Override
            public R updateCartPrice(String source) {
                return R.fail("修改当前用户的购物车商品实时价格失败:" + throwable.getMessage());
            }

            @Override
            public R deleteCartCheckedList(String source) {
                return R.fail("删除已经完成下单的购物车商品失败:" + throwable.getMessage());
            }
        };
    }
}
